package com.tanhua.server.controller;

import com.tanhua.domain.vo.MomentVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.PublishVo;
import com.tanhua.domain.vo.VisitorVo;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MomentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MomentController {

    @Autowired
    private MomentService momentService;

    @Autowired
    private CommentService commentService;

    /**
     * 发布动态
     * @param vo
     * @param imageContent 上传的图片数组，必须与api的参数名一样
     * @return
     */
    @PostMapping
    public ResponseEntity postMoment(PublishVo vo, MultipartFile[] imageContent){
        momentService.postMoment(vo, imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询好友动态
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity queryFriendPublishList(@RequestParam(value = "page",defaultValue="1") Long page,
                                                 @RequestParam(value = "pagesize",defaultValue="10")Long pageSize){
        PageResult<MomentVo> pageResult = momentService.queryFriendPublishList(page,pageSize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 分页查询推荐给登录用户的动态
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity queryRecommendPublishList(@RequestParam(value = "page",defaultValue="1") Long page,
                                                 @RequestParam(value = "pagesize",defaultValue="10")Long pageSize){
        PageResult<MomentVo> pageResult = momentService.queryRecommendPublishList(page,pageSize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 分页查询我的动态
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity queryMyPublishList(@RequestParam(value = "page",defaultValue="1") Long page,
                                                 @RequestParam(value = "pagesize",defaultValue="10")Long pageSize,
                                             @RequestParam(value = "userId",required = false) Long userId){
        PageResult<MomentVo> pageResult = momentService.queryMyPublishList(userId,page,pageSize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 动态的点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{publishId}/like")
    public ResponseEntity like(@PathVariable String publishId){
        int likeCount = commentService.like(publishId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 动态的取消点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{publishId}/dislike")
    public ResponseEntity dislike(@PathVariable String publishId){
        int likeCount = commentService.dislike(publishId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 动态的喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{publishId}/love")
    public ResponseEntity love(@PathVariable String publishId){
        int loveCount = commentService.love(publishId);
        return ResponseEntity.ok(loveCount);
    }

    /**
     * 动态的取消喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{publishId}/unlove")
    public ResponseEntity unlove(@PathVariable String publishId){
        int loveCount = commentService.unlove(publishId);
        return ResponseEntity.ok(loveCount);
    }

    /**
     * 首页中的 最近的访客
     * //TODO 第9天才实现
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        List<VisitorVo> list = momentService.query4Visitors();
        return ResponseEntity.ok(list);
    }

    /**
     * 查询单条动态信息
     * @param publishId
     * @return
     */
    @GetMapping("/{publishId}")
    public ResponseEntity findById(@PathVariable String publishId){
        MomentVo vo = momentService.findById(publishId);
        return ResponseEntity.ok(vo);
    }


}
